package wty;
//12.16 25.59
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Register {

    public static JPanel panel;

    public Register(){
        init();
    }

    public void init(){
        panel = new JPanel();
        panel.setLayout(new BorderLayout(20, 20));
        panel.setBackground(Color.YELLOW);

        JLabel label = new JLabel("Welcome to the Register!", JLabel.CENTER);
        label.setFont(new Font("", Font.BOLD, 36));
        label.setPreferredSize(new Dimension(0, 60)); // 设置标签的高度
        panel.add(label, BorderLayout.NORTH); // 将标签添加到上面

        // 创建中间输入面板
        JPanel inputPanel = new JPanel();

        inputPanel.setLayout(new GridLayout(5, 2, 10, 10));  // 改为4行2列的网格布局，间隔10像素
        inputPanel.setBackground(Color.ORANGE);
        inputPanel.setBorder(new EmptyBorder(10, 0, 10, 10)); // 设置上下边距为10像素

        // 创建昵称输入
        JLabel useNameLabel = new JLabel("Use Name:");
        useNameLabel.setFont(new Font("Arial", Font.PLAIN, 28));
        JTextField useNameField = new JTextField();
        useNameField.setFont(new Font("Arial", Font.PLAIN, 28));

        // 创建用户ID输入
        JLabel userLabel = new JLabel("User ID:");
        userLabel.setFont(new Font("Arial", Font.PLAIN, 28));
        JTextField userField = new JTextField();
        userField.setFont(new Font("SimSun", Font.PLAIN, 28));

        // 创建密码输入
        JLabel passwordLabel = new JLabel("Password:");
        passwordLabel.setFont(new Font("Arial", Font.PLAIN, 28));
        JPasswordField passwordField = new JPasswordField();
        passwordField.setFont(new Font("Arial", Font.PLAIN, 28));

        // 创建确认密码输入
        JLabel confirmPasswordLabel = new JLabel("Confirm Password:");
        confirmPasswordLabel.setFont(new Font("Arial", Font.PLAIN, 28));
        JPasswordField confirmPasswordField = new JPasswordField();
        confirmPasswordField.setFont(new Font("Arial", Font.PLAIN, 28));

        // 将各个组件添加到输入面板
        inputPanel.add(useNameLabel);
        inputPanel.add(useNameField);
        inputPanel.add(userLabel);
        inputPanel.add(userField);
        inputPanel.add(passwordLabel);
        inputPanel.add(passwordField);
        inputPanel.add(confirmPasswordLabel);
        inputPanel.add(confirmPasswordField);

        // 创建电话号码输入
        JLabel phoneLabel = new JLabel("Phone Number:");
        phoneLabel.setFont(new Font("Arial", Font.PLAIN, 28));
        JTextField phoneField = new JTextField();
        phoneField.setFont(new Font("Arial", Font.PLAIN, 28));

        // 将电话号码输入添加到输入面板
        inputPanel.add(phoneLabel);
        inputPanel.add(phoneField);

        // 将输入面板添加到中部
        panel.add(inputPanel, BorderLayout.CENTER);

        // 创建注册按钮
        JButton registerButton = new JButton("Register");
        registerButton.setFont(new Font("Arial", Font.PLAIN, 28));

        //-------------------------------------------------------------
//        Register_SQL tmpclass = new Register_SQL();
//        tmpclass.mysql(useNameField,userField,passwordField,phoneField);
        //-------------------------------------------------------------

        registerButton.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {

                        String tmp = useNameField.getText();

                        // 如果格式正确，跳转到登录界面

                        if (checksame(passwordField, confirmPasswordField)
                                && checkpattern(userField,passwordField)
                                && check_phoneNumber(phoneField)) {

//                            mysql(useNameField,userField,passwordField,phoneField);
//                            Register_SQL tmpclass = new Register_SQL();
//                            tmpclass.mysql(useNameField,userField,passwordField,phoneField);
                            Register_SQL.mysql(useNameField,userField,passwordField,phoneField);

                            Register.panel.setVisible(false);
                            Login.panel.setVisible(true);

                            //设置为空
                            useNameField.setText("");
                            userField.setText("");
                            passwordField.setText("");
                            confirmPasswordField.setText("");
                            phoneField.setText("");

                        } else {
                            label.setText("Format error!");
                            label.setForeground(Color.RED);
                        }
                    }
                }
        );

        JPanel southPanel = new JPanel();
        southPanel.setLayout(new BorderLayout());
        southPanel.add(registerButton, BorderLayout.CENTER);

        JButton backButton = new JButton("Back");
        backButton.setFont(new Font("Arial", Font.PLAIN, 28));
        backButton.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Register.panel.setVisible(false);
                        Login.panel.setVisible(true);
                    }
                }
        );
        southPanel.add(backButton, BorderLayout.EAST);

        // 将注册按钮添加到底部
        panel.add(southPanel, BorderLayout.SOUTH);

//        Register.panel.setVisible(false);
    }


    private boolean checksame(JPasswordField passwordField, JPasswordField confirmPasswordField) {
//        System.out.println("进入确认密码");
        char[] password = passwordField.getPassword();
        char[] confirmPassword = confirmPasswordField.getPassword();

        if(!Arrays.equals(password, confirmPassword)){
            System.out.println("确认密码错误");
            return false;
        }
        System.out.println("确认密码正确");
        return true;
    }

    private boolean checkpattern(JTextField userField,JPasswordField passwordField) {
//        System.out.println("进入格式");
        //分别验证用户名和密码符合格式

        //学号的格式，1950-2024这之间的4个数字，代表年份加上6个任意数字
        Pattern pattern_userField = Pattern.compile("^(19[5-9][0-9]|20[0-1][0-9]|202[0-4])\\d{6}$");

        Matcher matcher_userField = pattern_userField.matcher(userField.getText());

        //密码为6-12位，至少含大写字母、小写字母、数字、特殊字符中的至少两种

        //(?=.*[a-z])：确保至少有一个小写字母。
        //(?=.*[A-Z])：确保至少有一个大写字母。
        //(?=.*\\d)：确保至少有一个数字。
        //(?=.*[@*-_#])：确保至少有一个特殊字符（非字母和数字）。
        //(?=): 正向前瞻
        //C4 2=6

        Pattern pattern_passwordField = Pattern.compile(
                  "^(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\\d@*-_#]{6,12}$" +
                        "|^(?=.*[a-z])(?=.*\\d)[a-zA-Z\\d@*-_#]{6,12}$" +
                        "|^(?=.*[a-z])(?=.*[@*-_#])[a-zA-Z\\d@*-_#]{6,12}$"+
                        "|^(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d@*-_#]{6,12}$"+
                        "|^(?=.*[A-Z])(?=.*[@*-_#])[a-zA-Z\\d@*-_#]{6,12}$"+
                        "|^(?=.*\\d)(?=.*[@*-_#])[a-zA-Z\\d@*-_#]{6,12}$"
        );

//        Pattern pattern_passwordField = Pattern.compile(
//                        "^(?=.*[a-z])(?=.*\\d)[a-zA-Z\\d@*-_#]{6,12}$"
//        );


        char[] tmp = passwordField.getPassword();
        Matcher matcher_passwordField = pattern_passwordField.matcher(new String(tmp));

        if(!matcher_userField.matches()){
            System.out.println("ID匹配失败");
        }
        if(!matcher_passwordField.find()){
            System.out.println("密码匹配失败");
        }

        if(matcher_passwordField.matches()&&matcher_userField.matches()){
//            System.out.println("格式正确");
            return true;
        }

//        System.out.println("验证用户名和密码符合格式错误");
        return false;
    }

    private boolean check_phoneNumber(JTextField phoneField) {
//        System.out.println("进入电话");
        Pattern pattern_phoneField = Pattern.compile("^1[3-9]\\d{9}$");
        Matcher matcher_phoneField = pattern_phoneField.matcher(phoneField.getText());
        if(matcher_phoneField.matches()){
//            System.out.println("电话正确");
            return true;
        }
//        System.out.println("检查电话号码错误");
        return false;
    }

}
